<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>
			兄弟海鲜管理系统
		</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="./css/x-admin.css" media="all">

		<link href="css/xtiper.css" type="text/css" rel="stylesheet" />
		<script src="js/xtiper.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="js/rmodal.js"></script>

	</head>

	<body onload="findall()">
		<div class="x-nav">
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
		</div>
		<div class="x-body">
			<div style="margin-bottom: 15px;">
				<label class="layui-form-label">查询分类</label>
				<div class="layui-input-inline">
					<input type="text" id="typeName" name="typeName" placeholder="分类名称" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-input-inline" style="width:80px">
					<button class="layui-btn" onclick="search()"><i class="layui-icon">&#xe615;</i></button>
				</div>
			</div>
			<xblock>
				<button class="layui-btn layui-btn-danger" onclick="batchdelete()">
            		批量删除</button>

				<button class="layui-btn" onclick="inserttypes()">
            		<i class="layui-icon">&#xe608;</i>添加</button>
				<span class="x-right" style="line-height:40px">共：<span id="total" name="total"  ></span> 条数据</span>
			</xblock>
			<table class="layui-table">
				<thead>
					<tr>
						<th>
						</th>
						<th>
							ID
						</th>
						<th>
							分类名称
						</th>
						<th>
							操作
						</th>
					</tr>
				</thead>
				<tbody id="typeslist">
				</tbody>
			</table>
			
		</div>
		<script src="./lib/layui/layui.js" charset="utf-8"></script>
		<script src="./js/x-layui.js" charset="utf-8"></script>
		<script type="text/javascript">
			
			function findall() {
				var url = 'http://127.0.0.1:8080/type/findall';
				$.ajax({
					type: "get", //post方式传输，比get更加密安全
					url: url, //请求地址
					contentType: "application/json;charsetset=UTF-8",
					success: function(data) {
						var sum = 0;
						var msg = "";
						var a = 1;
						for(var i = 0; i < data.length; i++) {
							msg += '<tr> <td>';
							msg += '<input type=\"checkbox\" value=\"' + data[i].typeId + '\" id=\"typeId\" name=\"typeId\"></td> ';
							msg += '<td>' + a + '</td> ';
							msg += '<td>' + data[i].typeName + '</td>';
							msg += '<td class=\"td-manage\"> ';
							msg += ' <a title=\"编辑\"  class=\"ml-5\" style=\"text-decoration:none\" onclick=\"update(' + data[i].typeId + ')\">';
							msg += '<i class=\"layui-icon\"><img src=\"images/修改.png\" style="width: 25;\" height=\"25\"></i>';
							msg += '</a>';
							msg += ' &nbsp;&nbsp;';
							msg += '<a class=\"xbutton\" title=\"删除分类\" onclick=\"deletebyid(' + data[i].typeId + ')\" >';
							msg += '<i><img src=\"images/删除.png\" style=\"width: 25;\" height=\"25\"></i>';
							msg += '</a> </td> </tr>';
							a++;
							sum++;
						}
						$("#typeslist").html(msg);
						$("#total").text(sum);
					},
					error: function() {
						xtip.msg("失败");
					}
				})
			}
		</script>
		<script type="text/javascript">
				function search() {
					var typeName = $("#typeName").val();
					var url = 'http://127.0.0.1:8080/type/findPage?page=1&size=10';
					var settings = {
						"url": url,
						"method": "POST",
						"timeout": 0,
						"headers": {
							"Content-Type": "application/json"
						},
						"data": JSON.stringify({
							"name": typeName
						}),
					};
					$.ajax(settings).done(function (response) {
						if (response!="") {
							$("#typeslist").empty();
							var sum = 0;
						var msg = "";
						var a = 1;
						for(var i = 0; i < response.list.length; i++) {
							msg += '<tr> <td>';
							msg += '<input type=\"checkbox\" value=\"' + response.list[i].typeId + '\" id=\"typeId\" name=\"typeId\"></td> ';
							msg += '<td>' + a + '</td> ';
							msg += '<td>' + response.list[i].typeName + '</td>';
							msg += '<td class=\"td-manage\"> ';
							msg += ' <a title=\"编辑\"  class=\"ml-5\" style=\"text-decoration:none\" onclick=\"update(' + response.list[i].typeId + ')\">';
							msg += '<i class=\"layui-icon\"><img src=\"images/修改.png\" style="width: 25;\" height=\"25\"></i>';
							msg += '</a>';
							msg += ' &nbsp;&nbsp;';
							msg += '<a class=\"xbutton\" title=\"删除分类\" onclick=\"deletebyid(' + response.list[i].typeId + ')\" >';
							msg += '<i><img src=\"images/删除.png\" style=\"width: 25;\" height=\"25\"></i>';
							msg += '</a> </td> </tr>';
							a++;
							sum++;
						}
						$("#typeslist").html(msg);
						$("#total").text(sum);
						}else{
							xtip.msg("没有数据");
						}
				})
				}
			</script>
		<script src="js/jquery.min.js"></script>

		<script type="text/javascript">
			function update(typeId) {
				var url = 'http://127.0.0.1:8080/type/findTypes?typeId=' + typeId;
				$.ajax({
					type: "post", //post方式传输，比get更加密安全
					url: url, //请求地址
					async: false,
					success: function(data) {
						xtip.win({
							tip: '<input id="inputtype" name="inputtype" type="text" style="width: 180px;height: 28px;border: 1px solid #E6E6E6;" placeholder="' + data.typeName + '"/>',
							icon: 'w',
							title: '添加分类',
							btn: ['确认修改', '取消修改'],
							btn1: function() {
								var ins = $("#inputtype").val();
								var url = 'http://127.0.0.1:8080/type/findName?typeName=' + ins;
								var settings = {
									"url": url,
									"method": "GET",
									"timeout": 0,
								};
								$.ajax(settings).done(function(response) {
									if(response == "") {
										save(typeId, ins);
									} else {
										xtip.msg("失败");
									}
								});
							}
						});
					}
				})
			}
		</script>
		<script type="text/javascript">
			function batchdelete(){
				var arr = [];
					$("input:checkbox[name='typeId']:checked").each(function() {
						arr.push($(this).val());
					});
					var settings = {
						"url": "http://127.0.0.1:8080/type/batch",
						"method": "POST",
						"timeout": 0,
						"headers": {
							"Content-Type": "application/json"
						},
						"data": JSON.stringify(arr),
					};

					$.ajax(settings).done(function(response) {
						xtip.msg("批量删除成功，稍后刷新",{times:1});
						location.href = "./species-type-list.html";
					});
			}
		</script>

		<script type="text/javascript">
			function deletebyid(typeId) {
				xtip.confirm('确认删除吗？', function() {
					var url = 'http://127.0.0.1:8080/type/delete?typeId=' + typeId;
					var settings = {
						"url": url,
						"method": "GET",
						"timeout": 0,
					};

					$.ajax(settings).done(function(response) {
						xtip.msg('删除成功！',{times:1});
						location.replace(location.href);
					});
				});
			}
		</script>

		<script type="text/javascript">
			function inserttypes() {
				xtip.win({
					tip: '<input id="inputtype" name="inputtype" type="text" style="width: 180px;height: 28px;border: 1px solid #E6E6E6;" placeholder="输入名称"/>',
					icon: 'w',
					title: '添加分类',
					btn: ['确认添加', '取消添加'],
					btn1: function() {
						var ins = $("#inputtype").val();
						var url = 'http://127.0.0.1:8080/type/findName?typeName=' + ins;
						var settings = {
							"url": url,
							"method": "GET",
							"timeout": 0,
						};
						$.ajax(settings).done(function(response) {
							if(response == "") {
								insert(ins);
							} else {
								xtip.msg("已存在",{times:1});
							}
						});
					},
				});
			}
		</script>
		<script type="text/javascript">
			function insert(typeName) {
				var url = 'http://127.0.0.1:8080/type/insert?typeName=' + typeName;
				var settings = {
					"url": url,
					"method": "GET",
					"timeout": 0,
				};
				$.ajax(settings).done(function(response) {
					if(response == 1) {
						xtip.msg('添加成功！',{times:1});
						location.replace(location.href);
					} else {
						xtip.msg('添加失败！',{times:1});
					}
				});
			}
		</script>

		<script type="text/javascript">
			function save(typeId, typeName) {
				var settings = {
					"url": "http://localhost:8080/type/update",
					"method": "POST",
					"timeout": 0,
					"headers": {
						"Content-Type": "application/json"
					},
					"data": JSON.stringify({
						"typeId": typeId,
						"typeName": typeName
					}),
				};

				$.ajax(settings).done(function(response) {
					if(response == 1) {
						xtip.msg('修改成功！',{times:1});
						location.replace(location.href);
					} else {
						xtip.msg('失败，存在订单使用该分类，若需修改成新分类，可进行删除再重新添加！',{times:1});
					}
				});
			}
		</script>

	</body>

</html>